<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>layui</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="../../../layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layui/css/public.css" media="all">
		<style>
			body {
				background-color: #ffffff;
			}
		</style>
	</head>

	<body>
		<div class="layui-form layuimini-form" style="display: flex; flex-direction: column; align-items: center;">
			<div class="layui-form-item" style="margin-top:70px;">
				<input type="file" class="layui-btn" id="upload_excel"></input>
				<!-- <i class="layui-icon"></i>导入Excel -->
			</div>
			<div id="studentprogress" class="layui-progress" style="width:60%; margin-top:30px;display:none;"
				lay-showPercent="yes" lay-filter="studentprogress">
				<div id="c-p" class="layui-progress-bar" lay-percent="10%"></div>
			</div>
			<div class="layui-form-item" style="margin-top:30px;">

				<div><span>1、请按照导入模板填写数据，</span><a href="">点击下载导入模板</a></div>
				<div> 2、支持导入扩展名为xls、xlsx的文件</div>
			</div>
			<!-- <div class="layui-form-item" style="margin-top:30px;">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认添加</button>

        </div> -->
		</div>
		<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
		<script src="../../../lib/layui_exts/excel.js" charset="utf-8"></script>
		<script src="../../../js/lay-config.js" utf-8"></script>
		<script>
			layui.config({
				base: '../../../lib/layui_exts/',
			}).extend({
				excel: 'excel',
			}).use(['form', 'excel', 'element'], function() {
				var form = layui.form,
					layer = layui.layer,
					element = layui.element,
					$ = layui.$;
				var token = localStorage.getItem("token");
				var userid = localStorage.getItem("userId");
				//var schoolname = localStorage.getItem("schoolName");
				//console.log(schoolname)
				//监听提交
				// $(document).on('click', '#upload_excel', function(e) {
				//     console.log(e, '......')
				//     return false;
				// });
				$('#upload_excel').change(function(e) {
					const genRandom = (min, max) => (Math.random() * (max - min + 1) | 0) + min;
					$("#studentprogress").attr('style', 'width:60%; margin-top:30px;display:block;')
					var excel = layui.excel
					var files = e.target.files;
					console.log(userid,token,"token")
					try {
						var excel = layui.excel;
						excel.importExcel(files, {

							// 读取数据的同时梳理数据addDeviceList  
							fields: {
								'devicesn': 'A',
								'devicedesc': 'B',
								'city': 'C',
								'address': 'D',
								'jingdu': 'E',
								'weidu': 'F',
								/*'#barDemo': 'G',
								'schoolrollsn': 'H',
								'idtype': 'I',
								'idnumber': 'J',
								'homeaddress': 'K',
								'cardsn': 'L',*/
							}
						}, function(data) {
							console.log(data[0].Sheet1)
							var arr = "[";
							for (i = 0; i < data[0].Sheet1.length; i++) {
								arr +=
									`{devicesn: '${data[0].Sheet1[i].devicesn}',devicedesc: '${data[0].Sheet1[i].devicedesc}',jingdu: '${data[0].Sheet1[i].jingdu}',weidu: '${data[0].Sheet1[i].weidu}',address: '${data[0].Sheet1[i].address}',city: '${data[0].Sheet1[i].city}',}`
								if (i < data[0].Sheet1.length) {
									arr += ","
								}
							}
							arr += "]" //
							element.progress('studentprogress', genRandom(1, 100) + '%')

							console.log(JSON.stringify(arr)) 
							var strarr = '[{devicesn: '设备IMEI',devicedesc: '设备描述',jingdu: '经度',weidu: '纬度',address: '地址',city: '城市',},{devicesn: '3234',devicedesc: '',jingdu: '114.2368',weidu: '23.5643',address: '深圳市龙岗区',city: '深圳',},{devicesn: '3235',devicedesc: '',jingdu: '114.2368',weidu: '23.5643',address: '深圳市龙岗区',city: '深圳',},]'
							$.ajax({
								url: "https://mqtt.dyad888.com/api/device/addDeviceList",
contentType: "application/x-www-form-urlencoded",
								type: 'post',
								dataType: 'json',
								data: {
									//schoolName:schoolName,devicelist="+JSON.stringify(arr)+"&userid="+userid+"&token="+token,
									devicelist: strarr,
									userid: userid,
								token: token
								},
								success: function(data) {
									console.log(data,"测试查看数据")
									if (data.errorcode == 0) {
										layer.open({
											content: data.resultmsg,
											yes: function(index, layero) {
												// window.reload() 
												layer.closeAll()
												window.parent.location.reload();
											},
											cancel: function() {
												//右上角关闭回调
												return false //开启该代码可禁止点击该按钮关闭
											}
										});

										// setTimeout(function() {
										//    ; //疯狂模式，关闭所有层
										// }, 1000);
										//表格导入成功后，重载表格
										//
									} else {
										//表格导入失败后，重载文件上传
										layer.alert(data.resultmsg + "，失败：请重新上传", {
											icon: 2
										});
									}
								},
								error: function(msg) {
									layer.msg('请联系管理员!!!');
								}
							});
						})
					} catch (e) {
						layer.alert(e.message);
					}
				});
			});





			//模拟进度
			function toPercent(count, limit) {
				if (count % limit == 0) {
					return count / limit * 100 + "%";
				} else {
					return Math.round(count / limit * 1000) / 10 + "%";
				}
			}
			//随机数
			function getQueryString(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				var r = decodeURI(window.location.search).substr(1).match(reg);
				if (r != null) return unescape(r[2]);
				return null;
			}
		</script>
	</body>

</html>
